﻿using System;
using System.Collections.Generic;
using System.Text;

namespace FixEngine
{
    public class Utility
    {
        public const char SOH=(char)0x01;

        public static FixVersion ConvertBeginString2FixVersion(string value)
        {
            value = value.ToLower();
            switch (value)
            { 
                case "fix.4.0":
                    return FixVersion.Fix40;
                case "fix.4.1":
                    return FixVersion.Fix41;
                case "fix.4.2":
                    return FixVersion.Fix42;
                case "fix.4.3":
                    return FixVersion.Fix43;
                case "fix.4.4":
                    return FixVersion.Fix44;
                case "fix.5.0":
                    return FixVersion.Fix50;
                case "fix.5.0sp1":
                    return FixVersion.Fix50sp1;
                default:
                    return FixVersion.Unknown;
            }
        }
        public static string ConvertFixVersion2BeginString(FixVersion value)
        {
            switch (value)
            {
                case FixVersion.Fix40:
                    return "FIX.4.0";
                case FixVersion.Fix41:
                    return "FIX.4.1";
                case FixVersion.Fix42:
                    return "FIX.4.2";
                case FixVersion.Fix43:
                    return "FIX.4.3";
                case FixVersion.Fix44:
                    return "FIX.4.4";
                case FixVersion.Fix50:
                    return "FIX.5.0";
                case FixVersion.Fix50sp1:
                    return "FIX.5.0sp1";
                default:
                    throw new ArgumentOutOfRangeException("Unsupported fix version");
            }
        }

        public static DataType ConvertXmlDataType2DataType(string xmlDataType)
        { 
            switch(xmlDataType.Trim())
            {
                case "int":
                    return DataType.Integer;
                case "float":
                    return DataType.Float;
                case "Length":
                    return DataType.Length;
                case "NumInGroup":
                    return DataType.NumInGroup;
                case "SeqNum":
                    return DataType.SeqNum;
                case "TagNum":
                    return DataType.TagNum;
                case "DayOfMonth":
                    return DataType.DayOfMonth;
                case "Qty":
                    return DataType.Qty;
                case "Price":
                    return DataType.Price;
                case "PriceOffset":
                    return DataType.PriceOffset;
                case "Amt":
                    return DataType.Amt;
                case "Percentage":
                    return DataType.Percentage;
                case "char":
                    return DataType.Char;
                case "Boolean":
                    return DataType.Boolean;
                case "String":
                    return DataType.String;
                case "MultipleValueString":
                    return DataType.MultipleValueString;
                case "Country":
                    return DataType.Country;
                case "Currency":
                    return DataType.Currency;
                case "Exchange":
                    return DataType.Exchange;
                case "month-year":
                    return DataType.MonthYear;
                case "UTCTimestamp":
                    return DataType.UTCTimestamp;
                case "UTCTimeOnly":
                    return DataType.UTCTimeOnly;
                case "UTCDateOnly":
                    return DataType.UTCDateOnly;
                case "LocalMktDate":
                    return DataType.LocalMktDate;
                case "data":
                    return DataType.Data;
                case "time":
                    return DataType.Time;
                default:
                    throw new InvalidCastException(string.Format("Unrecognizable data type:{0}",xmlDataType) );
            }
        }

        public static string GenerateCheckSum(string buf,int bufLen)
        {
            int idx;
            uint cks;

            for (idx = 0, cks = 0; idx < bufLen; cks += (uint)buf[idx++]) ;

            return (cks % 256).ToString("000");
        }
    }
}
